#include "CMusicWidget.h"
#include "ui_CMusicWidget.h"

CMusicWidget::CMusicWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CMusicWidget)
{
    ui->setupUi(this);

    readFile();
    initUI();
}

CMusicWidget::~CMusicWidget()
{
    delete ui;
}

void CMusicWidget::readFile()
{
    // 设置遍历目录
    str_pathmusic = "C:/Users/lenovo/Music/";
    QDir dir(str_pathmusic);
    // 设置文件过滤器
    QStringList nameFilters;
    // 设置文件过滤器的格式
    nameFilters << "*.mp3";
    // 将过滤后的文件名存入list中
    QStringList files = dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
    // 添加到listwidget中的item中
    ui->listWidget->addItems(files);
    // 把item加入到listwidget中
    ui->listWidget->sortItems();
    // 设置默认item
    p_item = ui->listWidget->item(0);
    p_item->setSelected(true);
    n_music_number = 0;
}

// 封装UI初始化，避免在构造中使用
void CMusicWidget::initUI()
{
    connect(ui->btnUp, SIGNAL(clicked()), this, SLOT(slot_btnUp_clicked()));
    connect(ui->btnPause, SIGNAL(clicked()), this, SLOT(slot_btnPause_clicked()));
    connect(ui->btnDown, SIGNAL(clicked()), this, SLOT(slot_btnDown_clicked()));
    connect(ui->listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(slot_musicListRowNum(int)));
    connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slot_musicListRowNum(QListWidgetItem*)));

    b_play_music = false;
    p_player = nullptr;

    p_player = new QMediaPlayer;
    if(p_player == nullptr)
        qDebug()<<"initUI:p_player分配失败！"<<endl;
}

void CMusicWidget::slot_musicListRowNum(int n)
{
//    qDebug()<<n<<endl;
//    qDebug()<<ui->listWidget->item(n)->text()<<endl;
    if(n < 0 || n >= ui->listWidget->count())
        return ;

    if(b_play_music)
        p_player->stop();

    n_music_number = n;
    str_music = ui->listWidget->item(n)->text();
    p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));

    if(b_play_music)
        p_player->play();
}

void CMusicWidget::slot_musicListRowNum(QListWidgetItem* p_n)
{
    p_item = p_n;
    p_item->setSelected(true);
    n_music_number = ui->listWidget->currentRow();// 获取下标
    str_music = p_item->text();

    p_player->stop();
    p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
    p_player->play();

    ui->btnPause->setStyleSheet("QPushButton#btnPause{background-image: url(:/res/start.png);}");
    b_play_music = true;
}

void CMusicWidget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    QStyleOption opt;
    opt.init(this);
    QPainter pt(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &pt, this);
}

void CMusicWidget::slot_btnUp_clicked()
{
    if(b_play_music)
        p_player->stop();

    if(n_music_number == 0)
    {
        int nMax = ui->listWidget->count();
        n_music_number = nMax - 1;// 设置为最后一曲
    }
    else
    {
        n_music_number--;
    }
    str_music = ui->listWidget->item(n_music_number)->text();
    p_item = ui->listWidget->item(n_music_number);
    p_item->setSelected(true);// 设置选中

    p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
    if(b_play_music)
        p_player->play();
}

void CMusicWidget::slot_btnPause_clicked()
{
    if(b_play_music)
    {
        ui->btnPause->setStyleSheet("QPushButton#btnPause{background-image: url(:/res/pause.png);}");
        b_play_music = false;
        p_player->pause();
    }
    else
    {
        ui->btnPause->setStyleSheet("QPushButton#btnPause{background-image: url(:/res/start.png);}");
        b_play_music = true;
        p_player->play();
    }
}

void CMusicWidget::slot_btnDown_clicked()
{
    if(b_play_music)
        p_player->stop();

    int nMax = ui->listWidget->count();
    if(n_music_number == nMax - 1)
    {
        n_music_number = 0;// 设置为最后一曲
    }
    else
    {
        n_music_number++;
    }
    str_music = ui->listWidget->item(n_music_number)->text();
    p_item = ui->listWidget->item(n_music_number);
    p_item->setSelected(true);// 设置选中

    p_player->setMedia(QUrl::fromLocalFile(str_pathmusic + str_music));
    if(b_play_music)
        p_player->play();
}
